<template>
  <div class="app-container">

    <el-card v-permission="['teacher']">
<!--      <el-row>
        <el-form ref="queryForm" inline size="small" :model="queryForm">
          <el-form-item label="所属班级" prop="classId">
            <my-cascader v-model="form.classId" :options="classroomTreeList" :level="2"></my-cascader>
          </el-form-item>
          <el-button icon="el-icon-search" size="small" type="primary" @click="onClickSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQueryForm">重置</el-button>
        </el-form>
      </el-row>-->
      <el-row>
        <el-button icon="el-icon-plus" size="small" type="primary" plain @click="onClickAdd">新增</el-button>
<!--        <el-button icon="el-icon-edit" size="small" type="success" plain @click="onClickEdit()"
                   :disabled="editDisabled">修改
        </el-button>-->
<!--        <el-button icon="el-icon-edit" size="small" type="danger" plain v-permission="['admin']">删除</el-button>-->
      </el-row>
    </el-card>
    <el-table
      v-loading="loading"
      :data="list"
      element-loading-text="加载中"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column align="center" label="ID" width="95" prop="id"></el-table-column>
      <el-table-column label="课程名" prop="schedule.course.name"></el-table-column>
      <el-table-column label="学年学期">
        <template v-slot="scope">
          <year-semester-show :year="scope.row.schedule.course.year" :semester="scope.row.schedule.course.semester"></year-semester-show>
        </template>
      </el-table-column>
      <el-table-column label="原教室" prop="rawClassroom.name"></el-table-column>
      <el-table-column label="新教室" prop="newClassroom.name"></el-table-column>
      <el-table-column label="原节次">
        <template v-slot="scope">
          {{ scope.row.rawSection | sectionFilter }}
        </template>
      </el-table-column>
      <el-table-column label="新节次">
        <template v-slot="scope">
          {{ scope.row.newSection | sectionFilter }}
        </template>
      </el-table-column>
      <el-table-column label="理由" prop="reason"></el-table-column>
      <el-table-column label="状态" prop="status">
        <template slot-scope="scope">
          <status-show data-name="apply_status" :value="scope.row.status"></status-show>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <template v-if="scope.row.status ===
           3">
            <el-button type="text" size="small" @click="handleEdit(scope.row.id)" v-permission="['teacher']">修改申请</el-button>

            <el-button type="text" size="small" @click="handleDeal(scope.row.id, 'AGREE')" v-permission="['admin']">同意</el-button>
            <el-button type="text" size="small" @click="handleDeal(scope.row.id, 'REFUSE')" v-permission="['admin']">拒绝</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-drawer
      :title="drawer.title"
      :visible.sync="drawer.show"
      :before-close="onDrawerClose"
      size="50%">
      <el-form :rules="formRules" ref="editForm" label-width="auto" :model="form" size="medium">
        <el-form-item label="课程节次选择" prop="scheduleId">
          <el-select v-model="form.scheduleId" filterable style="width: 100%" @change="onScheduleIdChange">
            <el-option
              :label="`${item.course.name}-${item.classroom.name}-周${item.section.day}(${item.section.startNum}-${item.section.endNum}节)`"
              :value="item.id" v-for="(item, index) in scheduleList"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="新教室" prop="newClassroomId">
          <my-cascader v-model="form.newClassroomId" :options="classroomTreeList" :level="2"></my-cascader>
        </el-form-item>
        <el-form-item label="新节次" prop="newSectionId">
          <el-select v-model="form.newSectionId" filterable>
            <el-option :label="`周${item.day}(${item.startNum}-${item.endNum}节)`" :value="item.id"
                       v-for="(item, index) in sectionList"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请理由" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入申请理由" type="textarea"></el-input>
        </el-form-item>
        <!--        <el-form-item label="课程状态" prop="status">
                  <el-radio-group v-model="form.status">
                    <el-radio :label="item.value" v-for="(item, index) in enums.apply_status">{{ item.label }}</el-radio>
                  </el-radio-group>
                </el-form-item>-->
      </el-form>
      <div class="form-drawer-footer">
        <el-button @click="onDrawerClose">取 消</el-button>
        <el-button type="primary" @click="onDrawerConfirm" :loading="drawer.confirmLoading">
          {{ drawer.confirmLoading ? '提交中 ...' : '确 定' }}
        </el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {addApply, dealApply, deleteOneApply, getApplyList, getOneApply, updateApply} from "@/api/apply";
import {getAllScheduleList} from "@/api/schedule";
import List from "@/mixins/list";
import ApplyValidator from "@/validator/ApplyValidator";
import {mapGetters} from "vuex";
import {getClassroomTreeList} from "@/api/build";
import {getAllSection} from "@/api/section";
import MyCascader from "@/components/MyCascader";
import SectionShow from "@/components/SectionShow";
import sectionShowMixin from "@/components/SectionShow/sectionShowMixin";
import StatusShow from "@/components/StatusShow";
import YearSemesterShow from "@/components/YearSemesterShow/YearSemesterShow";

export default {
  components: {YearSemesterShow, StatusShow, MyCascader},
  data() {
    return {
      queryListFun: getApplyList,
      addFun: addApply,
      updateFun: updateApply,
      queryOneFun: getOneApply,
      deleteOneFun: deleteOneApply,
      validator: ApplyValidator,
      queryForm: {
      },
      form: {
        scheduleId: null,
        newClassroomId: null,
        newSectionId: null,
        reason: ''
      },
      name: '调课申请',
      classroomTreeList: [],
      sectionList: [],
      scheduleList: []
    }
  },
  mixins: [
    List,
    sectionShowMixin
  ],
  mounted() {
    getAllScheduleList().then(res => {
      this.scheduleList = res.data
    })
    getClassroomTreeList().then(res => {
      const classroomTreeList = []
      for (let i of res.data) {
        const children = []
        for (let j of i.classrooms) {
          children.push({
            id: j.id,
            value: j.id,
            label: j.name,
          })
        }
        classroomTreeList.push({
          id: i.id,
          value: i.id,
          label: i.name,
          children
        })
      }
      this.classroomTreeList = classroomTreeList
    })
    getAllSection().then(res => {
      this.sectionList = res.data
    })
  },
  computed: {
    ...mapGetters(['enums'])
  },
  methods: {
    onScheduleIdChange(id) {
      for (let i of this.scheduleList) {
        if (i.id === id) {
          this.form.newClassroomId = i.classroomId
          this.form.newSectionId = i.sectionId
        }
      }
    },
    handleDeal(id, type) {
      dealApply(id, type).then(res => {
        this.$message({
          type: 'success',
          message: res.msg
        })
        this.loadData({
          page: this.page,
          pageSize: this.pageSize
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
